package com.htdata.iiot.tools;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;

import com.alibaba.fastjson.JSONObject;

public class HTTPUtil {
	public static String userAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0";
	public static String accept = "*/*";
	public static String connection = "keep-alive";
	public static int timeout = 10000; // 10 seconds

	public static void post(String url, String apiKey, Object obj) {
		URL realUrl = null;
		HttpURLConnection conn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String line = null;
		String result = "";
		long st = 0;

		try {
			realUrl = new URL(url);
			conn = (HttpURLConnection) realUrl.openConnection();
			conn.setRequestMethod("POST");
			conn.setRequestProperty("user-agent", userAgent);
			conn.setRequestProperty("accept", accept);
			conn.setRequestProperty("connection", connection);
			conn.setRequestProperty("Content-Type", "application/json");

			if (apiKey != null && !apiKey.isEmpty()) {
				conn.setRequestProperty("apiKey", apiKey);
			}
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setReadTimeout(timeout);
			conn.connect();

			out = new PrintWriter(conn.getOutputStream());
			out.print(JSONObject.toJSONString(obj));
			out.flush();
			st = System.currentTimeMillis();

			// 定义BufferedReader输入流来读取URL的响应
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			while ((line = in.readLine()) != null) {
				result += "/n" + line;
			}

			System.out.println(result);
		} catch (SocketTimeoutException e) {
			System.out.println("Method:POST URL:" + url + " TimeoutException: " + (System.currentTimeMillis() - st));
		} catch (Exception e) {
			System.out.println("OtherException" + e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}

				if (in != null) {
					in.close();
				}

				if (conn != null) {
					conn.disconnect();
				}
			} catch (Exception e) {
			}
		}
	}

	public static void delete(String url, String apiKey) {
		URL realUrl = null;
		HttpURLConnection conn = null;
		BufferedReader in = null;
		String line = null;
		String result = "";
		long st = 0;

		try {
			realUrl = new URL(url);
			conn = (HttpURLConnection) realUrl.openConnection();
			conn.setRequestMethod("DELETE");
			conn.setRequestProperty("user-agent", userAgent);
			conn.setRequestProperty("accept", accept);
			conn.setRequestProperty("connection", connection);
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			if (apiKey != null && !apiKey.isEmpty()) {
				conn.setRequestProperty("apiKey", apiKey);
			}

			conn.setDoOutput(false);
			conn.setDoInput(true);
			conn.setReadTimeout(timeout);
			conn.connect();
			st = System.currentTimeMillis();

			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			while ((line = in.readLine()) != null) {
				result += "/n" + line;
			}

			System.out.println(result);
		} catch (SocketTimeoutException e) {
			System.out.println("Method:DELETE URL:" + url + " TimeoutException: " + (System.currentTimeMillis() - st));
		} catch (Exception e) {
			System.out.println("OtherException" + e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}

				if (conn != null) {
					conn.disconnect();
				}
			} catch (Exception e) {
			}
		}
	}

	public static void put(String url, String apiKey, Object obj) {
		URL realUrl = null;
		HttpURLConnection conn = null;
		PrintWriter out = null;
		BufferedReader in = null;
		String line = null;
		String result = "";
		long st = 0;

		try {
			realUrl = new URL(url);
			conn = (HttpURLConnection) realUrl.openConnection();
			conn.setRequestMethod("PUT");
			conn.setRequestProperty("user-agent", userAgent);
			conn.setRequestProperty("accept", accept);
			conn.setRequestProperty("connection", connection);
			conn.setRequestProperty("Content-Type", "application/json");

			if (apiKey != null && !apiKey.isEmpty()) {
				conn.setRequestProperty("apiKey", apiKey);
			}

			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setReadTimeout(timeout);
			conn.connect();
			st = System.currentTimeMillis();

			out = new PrintWriter(conn.getOutputStream());
			out.print(JSONObject.toJSONString(obj));
			out.flush();

			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			while ((line = in.readLine()) != null) {
				result += "/n" + line;
			}

			System.out.println(result);
		} catch (SocketTimeoutException e) {
			System.out.println("Method:PUT URL:" + url + " TimeoutException: " + (System.currentTimeMillis() - st));
		} catch (Exception e) {
			System.out.println("OtherException" + e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (out != null) {
					out.close();
				}

				if (in != null) {
					in.close();
				}

				if (conn != null) {
					conn.disconnect();
				}
			} catch (Exception e) {
			}
		}
	}

	public static void get(String url, String apiKey) {
		URL realUrl = null;
		HttpURLConnection conn = null;
		BufferedReader in = null;
		String line = null;
		StringBuilder result = null;
		long st = 0;

		try {
			realUrl = new URL(url);
			conn = (HttpURLConnection) realUrl.openConnection();
			conn.setRequestMethod("GET");
			conn.setRequestProperty("user-agent", userAgent);
			conn.setRequestProperty("accept", accept);
			conn.setRequestProperty("connection", connection);
			conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

			if (apiKey != null && !apiKey.isEmpty()) {
				conn.setRequestProperty("apiKey", apiKey);
			}
			conn.setDoOutput(false);
			conn.setDoInput(true);
			conn.setReadTimeout(timeout);
			conn.connect();
			st = System.currentTimeMillis();

			result = new StringBuilder(url);
			in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));

			while ((line = in.readLine()) != null) {
				result.append("\n\t\t");
				result.append(line);
			}

			System.out.println(new String(result));
		} catch (SocketTimeoutException e) {
			System.out.println("Method:GET URL:" + url + " TimeoutException: " + (System.currentTimeMillis() - st));
		} catch (Exception e) {
			System.out.println("OtherException" + e.getMessage());
			e.printStackTrace();
		} finally {
			try {
				if (in != null) {
					in.close();
				}

				if (conn != null) {
					conn.disconnect();
				}
			} catch (Exception e) {
			}
		}
	}
}